package com.ruoyi.server.login;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.server.enums.AddConstants5;
import com.ruoyi.server.enums.CmdConstants;
import com.ruoyi.server.enums.GoLiveConstants2;
import com.ruoyi.server.person.PassageWayUser;
import com.ruoyi.server.pkg.SendMsg;
import com.ruoyi.server.read.PrivateReader;
import com.ruoyi.server.thread.PullReceiptReader;
import com.ruoyi.server.utils.OnlineUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Objects;

/**
 *  集中写出处理
 *
 * @author OpenACloud
 * @create 2024/6/7 16:12
 */
public class FocusWriterReader extends SimpleChannelInboundHandler<PongWebSocketFrame> {

    private RedisCache redisCache;
    private PrivateReader privateReader;

    public FocusWriterReader(){

    }

    public FocusWriterReader(PrivateReader privateReader, RedisCache redisCache){
        this.redisCache = redisCache;
        this.privateReader = privateReader;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if(evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) {
            PassageWayUser passageWayUser = ctx.attr(OnlineUtil.PASSAGE_WAR_USER).get();
            OnlineUtil.ONLINE_MAPS.put(passageWayUser.getUserId(), ctx);

            SendMsg sendMsg = new SendMsg();
            sendMsg.CMD = GoLiveConstants2.GO_LIVE;
            sendMsg.STATUS = GoLiveConstants2.GO_LIVE_STATUS;
            sendMsg.SENDID = -1L;
            sendMsg.CONTEXT = "SYSTEM GO LIVE";
            sendMsg.TIME = LocalDateTime.now();
            ChannelFuture writeAndFlush = ctx.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(sendMsg)));
            writeAndFlush.addListener(new PullReceiptReader(ctx, sendMsg, privateReader));


            //TODO, 这里我觉得不会有太多申请
            Collection<String> keys = redisCache.keys(AddConstants5.AP_FRIEND_MSG + passageWayUser.getUserId() + ":*");
            for (String userKey : keys) {
                SendMsg apSendMsg = redisCache.getCacheObject(userKey);
                ctx.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(apSendMsg)));
                redisCache.deleteObject(userKey);
            }
        }

        super.userEventTriggered(ctx, evt);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("FocusWriterReader Exception");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, PongWebSocketFrame webSocketFrame) throws Exception {
        System.out.println("-----------------------------------------------");
        System.out.println("这个handler 不应该流进来消息???? 你为什么能进来");
        System.out.println("-----------------------------------------------");
    }
}
